package com.example.g5project.controller;

import com.example.g5project.entity.Consultant;
import com.example.g5project.entity.frontEndObject.ConsultInfoFrontObject;
import com.example.g5project.entity.frontEndObject.ConsultantIDwithConsultNumber;
import com.example.g5project.entity.frontEndObject.ConsultantIDwithMonthGrade;
import com.example.g5project.entity.frontEndObject.NewChat;
import com.example.g5project.entity.relation.ConsultInfo;
import com.example.g5project.exception.EntityNotExistException;
import com.example.g5project.others.ResponseValue;
import com.example.g5project.service.AdministratortService;
import com.example.g5project.service.ConsultantService;
import com.example.g5project.service.NewChatService;
import com.example.g5project.tools.EntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author Hu Zirui
 * @version 1.0.0
 * @ClassName ConsultantController.java
 * @Description TODO
 * @createTime 2022年03月23日 15:33:00
 */
@Api("咨询师接口操作")
@CrossOrigin
@RestController
@RequestMapping(value = "/consultant")
public class ConsultantController {
    private final ConsultantService consultantService;
    private final AdministratortService administratortService;
    private final NewChatService newChatService;

    public ConsultantController(ConsultantService consultantService, AdministratortService administratortService, NewChatService newChatService) {
        this.consultantService = consultantService;
        this.administratortService = administratortService;
        this.newChatService = newChatService;
    }

    @ApiOperation("根据ID获取咨询师信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getByID/{id}")
    public ResponseValue<?> getConsultantByID(@ApiParam(value = "需要获取信息的咨询师ID", required = true, example = "1") @PathVariable Integer id) throws EntityNotExistException {
        System.out.println(id);
        Consultant consultant = consultantService.getConsultantByID(id);
        return ResponseValue.success(consultant);
    }

    @ApiOperation("根据姓名获取咨询师信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getByName/{name}")
    public ResponseValue<?> getConsultantByName(@ApiParam(value = "需要获取信息的咨询师姓名", required = true, example = "czh") @PathVariable String name) throws EntityNotExistException {
        Consultant consultant = consultantService.getCounsultantByName(name);
        return ResponseValue.success(consultant);
    }

    @ApiOperation("获取所有咨询师信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getAll")
    public ResponseValue<?> getAllConsultant() throws EntityNotExistException {
        ArrayList<Consultant> consultantArrayList = consultantService.getAllConsultant();
        return ResponseValue.success(consultantArrayList);
    }

    @ApiOperation("根据ID硬删除咨询师")
    @RequestMapping(method = RequestMethod.GET, value = "/deleteByID/{id}")
    public ResponseValue<?> deleteConsultantByID(@PathVariable Integer id) {
        consultantService.deleteConsultantByID(id);
        return ResponseValue.success("success delete");
    }

    @ApiOperation("根据ID和新的信息，更新咨询师，现在可以传入一个不完整的Consultant类型了")
    @RequestMapping(method = RequestMethod.PUT, value = "/update/{id}")
    public ResponseValue<?> updateConsultantByID(@ApiParam(value = "需要获取信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id, @RequestBody @Validated Consultant consultant) throws EntityNotExistException {
        consultant.setConsultant_id(id);
        consultantService.updateConsultantByID(id, consultant);
        return ResponseValue.success("success update");
    }

    @ApiOperation("根据ID和新的信息，新增咨询师")
    @RequestMapping(method = RequestMethod.PUT, value = "/add")
    public ResponseValue<?> addConsultant(@ApiParam(value = "新增的咨询师", required = true) @RequestBody @Validated Consultant consultant) {
        consultantService.addConsultant(consultant);
        return ResponseValue.success("success add");
    }

    @ApiOperation("根据ID获取咨询师今日咨询数")
    @RequestMapping(method = RequestMethod.GET, value = "/getTodayConsultNumber/{id}")
    public ResponseValue<?> getTodayConsultNumber(@ApiParam(value = "需要获取信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id) throws EntityNotExistException {
        int count = consultantService.getTodayConsultNumber(id);
        return ResponseValue.success(count);
    }

    @ApiOperation("根据ID获取咨询师今日咨询时长")
    @RequestMapping(method = RequestMethod.GET, value = "/getTodayConsultTime/{id}")
    public ResponseValue<?> getTodayConsultTime(@ApiParam(value = "需要获取信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id) {
        String count = consultantService.getTodayConsultTime(id);
        return ResponseValue.success(count);
    }

    @ApiOperation("获取所有在线咨询师")
    @RequestMapping(method = RequestMethod.GET, value = "/getAllAlive")
    public ResponseValue<?> getAllAliveConsultant() throws EntityNotExistException {
        ArrayList<Consultant> consultantArrayList = consultantService.getAllAliveConsultant();
        return ResponseValue.success(consultantArrayList);
    }

    @ApiOperation("获取所有咨询师在线会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/getAllSessionNumber")
    public ResponseValue<?> getAllConsultSessionNumber() {
        Integer count = consultantService.getAllConsultSessionNumber();
        return ResponseValue.success(count);
    }

    @ApiOperation("根据当月咨询数量获取前k个咨询师")
    @RequestMapping(method = RequestMethod.GET, value = "/getTopKByConsultNumber/{k}/{month}")
    public ResponseValue<?> getFirstKrankConsultantByConsultNumber(@ApiParam(value = "需要的数量", required = true, example = "3") @PathVariable Integer k, @ApiParam(value = "月份", required = true, example = "1") @PathVariable Integer month) throws EntityNotExistException {
        ArrayList<ConsultantIDwithConsultNumber> consultantArrayList = consultantService.getFirstKrankConsultantByConsultNumber(k, month);
        return ResponseValue.success(consultantArrayList);
    }

    @ApiOperation("根据当月好评数量获取前k个咨询师")
    @RequestMapping(method = RequestMethod.GET, value = "/getTopKByGrade/{k}/{month}")
    public ResponseValue<?> getFirstKrankConsultantByMonthsGrade(@ApiParam(value = "需要的数量", required = true, example = "3") @PathVariable Integer k, @ApiParam(value = "月份", required = true, example = "1") @PathVariable Integer month) throws EntityNotExistException {
        ArrayList<ConsultantIDwithMonthGrade> consultantArrayList = consultantService.getFirstKrankConsultantByMonthsGrade(k, month);
        return ResponseValue.success(consultantArrayList);
    }

    @ApiOperation("增加一个在线执行会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/addalivesessionnumber/{id}")
    public ResponseValue<?> addAliveSessionNumber(@ApiParam(value = "需要增加信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id) {
        consultantService.addAliveSessionNumber(id);
        return ResponseValue.success("add success");
    }

    @ApiOperation("根据姓名增加一个在线执行会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/addalivesessionnumberByName/{name}")
    public ResponseValue<?> addAliveSessionNumberByName(@ApiParam(value = "需要增加信息的咨询师姓名", required = true, example = "-947912704") @PathVariable String name) {
        consultantService.addAliveSessionNumberByName(name);
        return ResponseValue.success("add success");
    }

    @ApiOperation("根据ID增加一个在线执行会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/minusalivesessionnumber/{id}")
    public ResponseValue<?> minusAliveSessionNumber(@ApiParam(value = "需要增加信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id) {
        consultantService.minusAliveSessionNumber(id);
        return ResponseValue.success("add success");
    }

    @ApiOperation("根据姓名减少一个在线执行会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/minusalivesessionnumberByName/{name}")
    public ResponseValue<?> minusAliveSessionNumberByName(@ApiParam(value = "需要增加信息的咨询师姓名", required = true, example = "-947912704") @PathVariable String name) {
        consultantService.minusAliveSessionNumberByName(name);
        return ResponseValue.success("add success");
    }

    @ApiOperation("增加一个今日执行会话数量")
    @RequestMapping(method = RequestMethod.GET, value = "/addtodaysessionnumber/{id}")
    public ResponseValue<?> addTodaySessionNumber(@ApiParam(value = "需要增加信息的咨询师ID", required = true, example = "-947912704") @PathVariable Integer id) {
        consultantService.addTodaySessionNumber(id);
        return ResponseValue.success("add success");
    }

    @ApiOperation("返回该咨询师本月排班信息")
    @RequestMapping(method = RequestMethod.GET, value = "/getschedule/{name}/{month}")
    public ResponseValue<?> getSchedule(@ApiParam(value = "需要增加信息的咨询师姓名", required = true, example = "-947912704") @PathVariable String name, @PathVariable String month) {
        ArrayList<String> scheduleDate = consultantService.getScheduleByMonth(name, month);
        return ResponseValue.success(scheduleDate);
    }

    @ApiOperation("返回该咨询师综合评价")
    @RequestMapping(method = RequestMethod.GET, value = "/getgrade/{name}")
    public ResponseValue<?> getGrade(@ApiParam(value = "需要增加信息的咨询师姓名", required = true, example = "-947912704") @PathVariable String name) {
        Integer grade = consultantService.getGradeByName(name);
        return ResponseValue.success(grade);
    }

    @ApiOperation("返回所有的咨询聊天内容")
    @RequestMapping(method = RequestMethod.GET, value = "/getallconsultmessage/{name}")
    public ResponseValue<?> getAllConsultMessage(@PathVariable String name) {
        EntityUtils entityUtils = new EntityUtils();
        ArrayList<ConsultInfo> arrayList = consultantService.getAllConsultMessage(name);
        ArrayList<ConsultInfoFrontObject> arrayList2 = new ArrayList<>();
        for (ConsultInfo consultInfo : arrayList) {
            ConsultInfoFrontObject consultInfoFrontObject = new ConsultInfoFrontObject();
            consultInfoFrontObject.setConsult_message_id(consultInfo.getConsult_message_id());
            consultInfoFrontObject.setId(consultInfo.getId());
            consultInfoFrontObject.setConsult_date(consultInfo.getStart_time());
            consultInfoFrontObject.setClient_name(consultInfo.getClient_name());
            consultInfoFrontObject.setEvaluation_content_from_client(consultInfo.getEvaluation_content_from_client());
            consultInfoFrontObject.setConsultant_grade(consultInfo.getConsultant_grade());
            consultInfoFrontObject.setSupervisor_name(consultInfo.getSupervisor_name());
            consultInfoFrontObject.setEnd_time(consultInfo.getEnd_time());
            consultInfoFrontObject.setHold_time(consultInfo.getHold_time());
            arrayList2.add(consultInfoFrontObject);
        }
        return ResponseValue.success(arrayList2);
    }

    @ApiOperation("设置咨询师状态")
    @RequestMapping(method = RequestMethod.GET, value = "/setStatus/{name}")
    public ResponseValue<?> setStatus(@ApiParam(value = "需要增加信息的咨询师姓名", required = true, example = "-947912704") @PathVariable String name) {
        consultantService.setStatus(name);
        return ResponseValue.success("设置状态成功");
    }


    @ApiOperation("咨询师累计完成咨询数量+1")
    @RequestMapping(method = RequestMethod.GET, value = "/increAllConsultNumber/{name}")
    public ResponseValue<?> increAllConsultNumber(@PathVariable String name) {
        System.out.println(name);
        consultantService.increAllConsultNumber(name);
        return ResponseValue.success("success add");
    }

    @ApiOperation("咨询师今日完成咨询数量+1")
    @RequestMapping(method = RequestMethod.GET, value = "/incretodayConsultNumber/{name}")
    public ResponseValue<?> incretodayConsultNumber(@PathVariable String name) {
        consultantService.incretodayConsultNumber(name);
        return ResponseValue.success("success add");
    }

    @ApiOperation("根据姓名从聊天表中更新咨询师的今日咨询时长")
    @RequestMapping(method = RequestMethod.GET, value = "/calculatetodayConsultTime/{name}")
    public ResponseValue<?> calculatetodayConsultTime(@PathVariable String name) throws ParseException {
        ArrayList<NewChat> arrayList = newChatService.getAllChatMsgByConsultantName(name);
        long Hours = 0;
        long Minutes = 0;
        long Seconds = 0;
        for (NewChat item : arrayList) {
            Date start_time = item.getStart_time();
            Date end_time = item.getEnd_time();
            long diff = end_time.getTime() - start_time.getTime();            //毫秒ms
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            Hours += diffHours;
            Minutes += diffMinutes;
            Seconds += diffSeconds;
            String diffSecondsStr = diffSeconds < 10 ? "0" + diffSeconds : "" + diffSeconds;
            String diffMinutesStr = diffMinutes < 10 ? "0" + diffMinutes : "" + diffMinutes;
            String diffHoursStr = diffHours < 10 ? "0" + diffHours : "" + diffHours;
            String hold_time = diffHoursStr + ":" + diffMinutesStr + ":" + diffSecondsStr;
            newChatService.updateHoldTimeByChatID(item.getId(), hold_time);
        }
        long s2m = Seconds / 60;
        Seconds %= 60;
        Minutes += s2m;
        long m2h = Minutes / 60;
        Minutes %= 60;
        Hours += m2h;
        String diffSecondsStr = Seconds < 10 ? "0" + Seconds : "" + Seconds;
        String diffMinutesStr = Minutes < 10 ? "0" + Minutes : "" + Minutes;
        String diffHoursStr = Hours < 10 ? "0" + Hours : "" + Hours;
        String hold_time = diffHoursStr + ":" + diffMinutesStr + ":" + diffSecondsStr;
        consultantService.updateHoldTimeByName(name, hold_time);
        return ResponseValue.success("success calculate");
    }

    @ApiOperation("增加咨询师今日咨询时长")
    @RequestMapping(method = RequestMethod.GET, value = "/incretodayConsultTime/{name}/{time}")
    public ResponseValue<?> incretodayConsultTime(@PathVariable String name, @PathVariable String time) {               // 默认今日咨询时长和历史咨询时长一样
        consultantService.increTodayConsultTime(name, time);
        return ResponseValue.success("success add");
    }

    @ApiOperation("增加咨询师历史咨询时长")
    @RequestMapping(method = RequestMethod.GET, value = "/increAllConsultTime/{name}/{time}")
    public ResponseValue<?> increAllConsultTime(@PathVariable String name, @PathVariable String time) {
        consultantService.increAllConsultTime(name, time);
        return ResponseValue.success("success add");
    }

}
